package com.ddwl.goods.service;

import com.ddwl.goods.dao.ItemDao;
import com.ddwl.goods.mapper.HistoryMapper;
import com.ddwl.goods.model.History;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.ItemSearchBo;
import com.ddwl.schema.vo.goods.ItemVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/7/15 12:55
 */
@Service
public class HistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    @Resource
    private ItemDao itemDao;


    public List<ItemVo> page(PageBo<ItemSearchBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize()).count(false);
        List<History> entities = historyMapper.findAll(pageBo.getParam());
        if (entities.isEmpty()) {
            return new ArrayList<>(0);
        }
        List<String> itemIds = entities.stream().map(History::getItemId).distinct().collect(Collectors.toList());
        return itemDao.findListByIds(itemIds);
//        List<Item> items = itemMapper.findByPks(itemIds);
//        Map<String, Item> itemMap = items.stream().collect(Collectors.toMap(Item::getId, Function.identity(), (k1, k2) -> k1));
//        return entities.stream().map(entity -> {
//            ItemVo vo = new ItemVo();
//            BeanUtils.copyProperties(itemMap.get(entity.getItemId()), vo);
//            return vo;
//        }).collect(Collectors.toList());
    }
}
